﻿using System;
using UnityEngine;

namespace Core
{
    /// <summary>
    /// 前缀 btn_XXX Button
    /// </summary>
    public class UIButton : UIWidget
    {
        private UnityEngine.UI.Button btn;
        private UnityEngine.UI.Image img;
        private Action clickAction;

        public event Action onClick;

        /// <summary>
        /// 不规则按钮
        /// </summary>
        public float AlphaThresholdUISkillButton
        {
            get => img.alphaHitTestMinimumThreshold;
            set => img.alphaHitTestMinimumThreshold = value;
        }

        public bool Interactable
        {
            get => btn.interactable;
            set => btn.interactable = value;
        }

        public override void Awake(GameObject go)
        {
            base.Awake(go);

            btn = go.GetComponent<UnityEngine.UI.Button>();
            img = btn.image;
            btn.onClick.AddListener(OnButtonClickHandler);
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            onClick = null;
            clickAction = null;

            btn.onClick.RemoveListener(OnButtonClickHandler);
            btn = null;
        }

        protected virtual void OnButtonClickHandler()
        {
            onClick?.Invoke();

            clickAction?.Invoke();
        }

        public void BindClick(Action action)
        {
            if (action == null)
            {
                return;
            }
            clickAction += action;
        }
    }
}
